<template>
  <div>
    <el-table :data="items">
      <el-table-column label="ID" prop="id"></el-table-column>

      <!-- <el-table-column label="" prop="name"></el-table-column> -->

      <el-table-column label="域名" prop="dn"> </el-table-column>

      <el-table-column label="公钥(SHA256SUM)" prop="todo"></el-table-column>

      <el-table-column label="生效时间">
        <template #default="scope">
          <div>不早于:{{ formatDateTime(scope.row.not_before) }}</div>
          <div>不晚于:{{ formatDateTime(scope.row.not_after) }}</div>
        </template>
      </el-table-column>

      <el-table-column label="剩余天数">
        <template #default="scope"> {{ formatTTL(scope.row) }} </template>
      </el-table-column>

      <el-table-column label="状态" prop="todo"></el-table-column>

      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            type="primary"
            link
            @click="handleClickItemDetail(scope.row)"
            >详情</el-button
          >

          <el-button type="danger" link>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
  
  <script>
export default {
  name: "domains-table",

  computed: {
    items() {
      return this.$store.getters["certificates/items"];
    },
  },

  methods: {
    getHomePageURL(row) {
      let dn = row.name;
      return "https://" + dn + "/";
    },

    handleClickItemDetail(row) {
      let dn = row.name;
      //   let path = "/domains/" + dn + "/certs";
      //   let location = this.$router.resolve({ path });
      console.log("detail of DN: " + dn);
    },

    formatDateTime(ms) {
      let d = new Date();
      d.setTime(ms);
      return d.toISOString();
    },

    formatTTL(row) {
      let now = new Date();
      let end = row.not_after;
      let ttl = end - now.getTime();
      if (ttl < 0) {
        return "已过期";
      }
      return Math.floor(ttl / (24 * 3600 * 1000));
    },
  },
};
</script>
  